//
//  LunBoTu.m
//  kr_36
//
//  Created by lmt on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "LunBoTu.h"

@implementation LunBoTu
- (void)dealloc
{
    [_timer release];
    [_scrollView release];
    [_page release];
    [super dealloc];
}
#pragma mark - 字符串数组轮播图
- (instancetype)initWithFrame:(CGRect)frame stringArray:(NSMutableArray *)arr
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc] init];
        [self addSubview:_scrollView];
        self.i = 0;
        
        _scrollView.frame = frame;
        // 设置大小
        _scrollView.contentSize = CGSizeMake(self.frame.size.width * arr.count, 0);
        // 设置代理
        _scrollView.delegate = self;
        // 整页滚
        _scrollView.pagingEnabled = YES;
        
        [arr addObject:arr.firstObject];
        
        // 轮播图添加图片
        for (NSInteger i = 0; i < arr.count; i++) {
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height)];
            [imgView sd_setImageWithURL:[NSURL URLWithString:arr[i]]];
            [_scrollView addSubview:imgView];
        }
        // 创建pageControl
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 50, 200, 40)];
        self.page.center = CGPointMake(self.center.x, self.page.center.y);
        self.page.numberOfPages = arr.count - 1;
        // self.page.backgroundColor = [UIColor blackColor];
        [self addSubview:self.page];
        
        // 记录数组个数
        self.arrCount = arr.count;
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(zidong) userInfo:nil repeats:YES];
    }
    return self;
}

#pragma mark - 自动播放装置 左向右滚
- (void)zidong
{
    // 因为有8张图 所以i值为0~7 八个数
    if (_i < _arrCount) {
        // 让scroll 自己变化 带动画的变
        [self.scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width * _i , 0) animated:YES];
        // 判断是第几个图 是最后图的时候
        if (_i == _arrCount - 1) {
            // 最后一个的pageControl跳到第一个 因为有8个图片 但是只有7个点
            _page.currentPage = 0;
        } else {
            // 其他的时候 page的点 也自己随着i变化
            _page.currentPage = _i;
        }
        _i++;
        
    } else {
        // 超过了 就让i变回 从头开始
        _i = 0;
        _page.currentPage = _i;
        // 最后一张假图变回第一张图 不用动画
        [_scrollView setContentOffset:CGPointMake(0 , 0) animated:NO];
        
    }
    
    
}

// 滚动开始
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 停止自动滚动
    [_timer invalidate];
    
    
}

// 滚动减速结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 匹配翻页页码
    
    if (_scrollView.contentOffset.x == _scrollView.frame.size.width * (_arrCount - 1)) {
        _scrollView.contentOffset = CGPointZero;
        _page.currentPage = 0;
    } else {
        _page.currentPage = _scrollView.contentOffset.x / _scrollView.frame.size.width;
    }
    
    _i = self.page.currentPage;
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(zidong) userInfo:nil repeats:YES];
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
